package com.qiguliuxing.dts.db.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.qiguliuxing.dts.db.dao.DtsCommentMapper;
import com.qiguliuxing.dts.db.dao.ex.CommentMapperEx;
import com.qiguliuxing.dts.db.domain.DtsComment;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class DtsCommentService {
	@Resource
	private DtsCommentMapper commentMapper;
	
	@Resource
	private CommentMapperEx commentMapperEx;

	public List<DtsComment> queryGoodsByGid(Integer id, int offset, int limit) {
		QueryWrapper<DtsComment> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsComment::getValueId, id);
		queryWrapper.eq("type", 0);
		queryWrapper.eq("deleted", false);
		queryWrapper.orderByDesc("add_time");
		IPage<DtsComment> iPage = new Page<>(offset, limit);
		return commentMapper.selectPage(iPage, queryWrapper).getRecords();
	}

	public List<DtsComment> query(Byte type, Integer valueId, Integer showType, Integer offset, Integer limit) {
		QueryWrapper<DtsComment> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsComment::getValueId, valueId);
		queryWrapper.eq("type", type);
		queryWrapper.eq("deleted", false);
		if (showType == 1) {
			queryWrapper.lambda().eq(DtsComment::getHasPicture, true);
		} else if (showType != 0){
			throw new RuntimeException("showType不支持");
		}
		queryWrapper.orderByDesc("add_time");
		IPage<DtsComment> iPage = new Page<>(offset, limit);
		return commentMapper.selectPage(iPage, queryWrapper).getRecords();
	}

	public int count(Byte type, Integer valueId, Integer showType) {
		QueryWrapper<DtsComment> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsComment::getValueId, valueId);
		queryWrapper.eq("type", type);
		queryWrapper.eq("deleted", false);
		if (showType == 1) {
			queryWrapper.lambda().eq(DtsComment::getHasPicture, true);
		} else if (showType != 0){
			throw new RuntimeException("showType不支持");
		}
		return commentMapper.selectCount(queryWrapper).intValue();
	}

	public int save(DtsComment comment) {
		comment.setAddTime(LocalDateTime.now());
		comment.setUpdateTime(LocalDateTime.now());
		return commentMapper.insert(comment);
	}

	public List<DtsComment> querySelective(String userId, String valueId, Integer page, Integer size, String sort,
			String order) {
		QueryWrapper<DtsComment> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("type", 2);
		if (StrUtil.isNotEmpty(valueId)) {
			queryWrapper.lambda().eq(DtsComment::getValueId, valueId);
			queryWrapper.eq("type", 0);
		}
		if (StrUtil.isNotEmpty(userId)) {
			queryWrapper.lambda().eq(DtsComment::getUserId, userId);
		}
		queryWrapper.eq("deleted", false);
		if (StrUtil.isNotEmpty(sort) && StrUtil.isNotEmpty(order)) {
			queryWrapper.orderByDesc(sort);
		}
		IPage<DtsComment> iPage = new Page<>(page, size);
		return commentMapper.selectPage(iPage, queryWrapper).getRecords();
	}

	public void deleteById(Integer id) {
		commentMapper.deleteById(id);
	}

	public String queryReply(Integer id) {
		QueryWrapper<DtsComment> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsComment::getValueId, id);
		queryWrapper.eq("type", 2);
		List<DtsComment> commentReply = commentMapper.selectList(queryWrapper);
		// 目前业务只支持回复一次
		if (commentReply.size() == 1) {
			return commentReply.get(0).getContent();
		}
		log.info("queryReply 拥有多条数据，id :{}, 处理结果：{}", id, JSON.toJSONString(commentReply));
		return null;
	}

	public DtsComment findById(Integer id) {
		return commentMapper.selectById(id);
	}

	/**
	 * 入驻店铺对应商品的评价
	 * @param brandIds
	 * @param userId
	 * @param valueId
	 * @param page
	 * @param sort
	 * @param order
	 * @return
	 */
	public List<DtsComment> queryBrandCommentSelective(List<Integer> brandIds, String userId, String valueId,
			Integer page, Integer size, String sort, String order) {
		
		String orderBySql = null;
		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			orderBySql = "o."+sort + " " + order;
		}
		
		String brandIdsSql = null;
		if (brandIds != null) {
			brandIdsSql = "";
			for (Integer brandId : brandIds) {
				brandIdsSql += "," + brandId;
			}
			brandIdsSql = " and g.brand_id in (" + brandIdsSql.substring(1) + ") ";
		}

		PageHelper.startPage(page, size);
		
		Byte type = (byte) 0;//品牌入驻管理员限定只查商品评论
		return commentMapperEx.queryBrandComment(type,userId,valueId,orderBySql,brandIdsSql);
	}
}
